Module: System::Collections::ProcishLambdas

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

Instance Method Summary collapse

Instance Method Details

#applyObject



35
36
37
# File 'lib/raskell/f.rb', line 35

def apply
  @@apply||= ->(f, *xs) { f.(*xs) }
end

#apply_fnObject



39
40
41
# File 'lib/raskell/f.rb', line 39

def apply_fn
  @@apply_fn||= -> (f, *args) { f.(*args)}
end

#apply_withObject



43
44
45
# File 'lib/raskell/f.rb', line 43

def apply_with
  @@apply_with||= ->(x,f) { f.(x) } # flip * apply
end

#apply_with2Object



31
32
33
# File 'lib/raskell/f.rb', line 31

def apply_with2
  @@apply_with2||= ->(y, f, x) { f.(x,y)}
end

#composeObject



47
48
49
# File 'lib/raskell/f.rb', line 47

def compose
  @@compose||= ->(f,g) { f * g }
end

#cstepObject

next_stream_fn yields Nothing for stop, and the next stream to go to otherwise yield_fn yields a Nothing for skip, and the next value to yield otherwise



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/raskell/f.rb', line 84

def cstep
  @@cstep||= ->(next_stream_fn, yield_fn) {
  ## next_stream_fn :: state -> next_fn -> Maybe(stream)
  ## yield_fn :: state -> Maybe(item)
  next_fn = ->(state) {
      next_stream = next_stream_fn.(state)
      to_yield = yield_fn.(state)
      if next_stream == Nothing
        [:done]
      elsif to_yield == Nothing
        [:skip, next_stream]
      else
        [:yield, to_yield, next_stream]
      end
    }
    next_fn
  }
end

#fixObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/raskell/f.rb', line 70

def fix
  @@fix||= ->(f, x) { 
    result = x
    next_result = f.(x)
    while result != next_result
      result = next_result
      next_result = f.(result)
    end
    result
  }
end

#flipObject



51
52
53
# File 'lib/raskell/f.rb', line 51

def flip
  @@flip||= -> (f,x,y) { f.(y,x) }
end

#repeatObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/raskell/f.rb', line 59

def repeat
  @@repeat||= ->(n, fn, x) {
    result = x
    count = n
    while count > 0
      result = fn.(result)
    end
    result
  }
end

#slfObject



55
56
57
# File 'lib/raskell/f.rb', line 55

def slf
  @@slf||= -> (f, x) { f.(x,x) } ## square = slf * times
end

#stepObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/raskell/f.rb', line 103

def step
  @@step||= ->(transition_fn, yield_fn) {
    ## transition_fn :: state -> Maybe(state)
    ## yield_fn :: state -> Maybe(item)
    next_fn = ->(state) {
      next_state = transition_fn.(state)
      to_yield = yield_fn.(state) if next_state != Nothing
      if next_state == Nothing
        [:done]
      elsif to_yield == Nothing
        [:skip, Stream.new(next_fn, next_state)]
      else
        [:yield, to_yield, Stream.new(next_fn, next_state)]
      end
    }
    next_fn
  }
end

#unfoldlObject



122
123
124
125
126
# File 'lib/raskell/f.rb', line 122

def unfoldl
  @@unfoldl||= -> (next_fn, stop_fn, seed) { 
    Stream.new(step.(->(state) { stop_fn.(state) ? Nothing : next_fn.(state) }, next_fn ), seed)
  }
end