Class: Obfusk::Monads::State

Inherits:
Object
  • Object
show all
Includes:
ADT, Obfusk::Monad
Defined in:
lib/obfusk/monads.rb

Defined Under Namespace

Classes: Pair

Class Method Summary collapse

Methods included from Obfusk::Monad

#>>, included

Methods included from ADT

#<=>, #==, #__adt_ctor__, #__adt_ctor_keys__, #__adt_ctor_name__, #__adt_data__, #_compare_data, #_eq_data, #clone, #eql?, included, #initialize, #inspect, #match, #to_s

Class Method Details

.bind_pass(m, &b) ⇒ Object



131
132
133
# File 'lib/obfusk/monads.rb', line 131

def self.bind_pass(m, &b)
  state { |s| x = m.run[s]; b[x.value].run[x.state] }
end

.eval(m, s) ⇒ Object

evaluate response with the given initial state and return the final value, discarding the final state



91
92
93
# File 'lib/obfusk/monads.rb', line 91

def self.eval(m, s)
  m.run[s].value
end

.exec(m, s) ⇒ Object

evaluate response with the given initial state and return the final state, discarding the final value



97
98
99
# File 'lib/obfusk/monads.rb', line 97

def self.exec(m, s)
  m.run[s].state
end

.getObject

get the state



108
109
110
# File 'lib/obfusk/monads.rb', line 108

def self.get
  state { |s| Pair s, s }
end

.gets(f = nil, &b) ⇒ Object

get a specific component of the state, using a projection function (or block)



124
125
126
# File 'lib/obfusk/monads.rb', line 124

def self.gets(f = nil, &b)
  state { |s| Pair (f || b)[s], s }
end

.modify(f = nil, &b) ⇒ Object

modify the state by applying a function (or block)



118
119
120
# File 'lib/obfusk/monads.rb', line 118

def self.modify(f = nil, &b)
  state { |s| Pair nil, (f || b)[s] }
end

.mreturn(x) ⇒ Object



128
129
130
# File 'lib/obfusk/monads.rb', line 128

def self.mreturn(x)
  state { |s| Pair x, s }
end

.put(s) ⇒ Object

set the state



113
114
115
# File 'lib/obfusk/monads.rb', line 113

def self.put(s)
  state { |_| Pair nil, s }
end

.state(f = nil, &b) ⇒ Object

construct a state monad computation from a function (or block)



85
86
87
# File 'lib/obfusk/monads.rb', line 85

def self.state(f = nil, &b)
  State f || b
end

.with(m, f = nil, &b) ⇒ Object

execute action on a state modified by applying a function (or block)



103
104
105
# File 'lib/obfusk/monads.rb', line 103

def self.with(m, f = nil, &b)
  state { |s| m.run[(f || b)[s]] }
end