仅类编程

图片



在我的帖子中的“纯”的Ruby实现数字(纯粹的“红宝石”,“在开发一个数字”),我提出了一个框架,允许像Ruby的平等操作,使用基本的东西true/ falsenil,块等。



但是,如果我们什么都没有呢?甚至基本运算符都喜欢ifwhile准备好应对纯粹的面向对象的疯狂。



构架



  • 我们可以定义类和方法
  • 我们应该编写代码,就像在Ruby中没有现成的类一样。试想一下,我们从绝对零开始。甚至nil不存在
  • 我们唯一可以使用的运算符是赋值(x = something)。


不,如果声明?认真吗 甚至处理器都有它!



条件语句很重要-它们是我们程序逻辑的基础。没有他们我们该如何应对?我想出了这样的解决方案:我们可以将逻辑集成到所有对象中



, Ruby - "Boolean". , (nil false Ruby;

false, 0 '' JS). , . .





, :



class BaseObject
  def if_branching(then_val, _else_val)
    then_val
  end
end


if_branching — . , , , then_val.



? null:



class NullObject < BaseObject
  def if_branching(_then_val, else_val)
    else_val
  end
end


, .



Ruby Object. BasicObject, . Object:



class NormalObject < BaseObject
end


, NormalObject. ( #null?).



If-



, if-:



class If < NormalObject
  def initialize(bool, then_val, else_val = NullObject.new)
    @result = bool.if_branching(then_val, else_val)
  end

  def result
    @result
  end
end


! . .



:



class Fries < NormalObject
end

class Ketchup < NormalObject
end

class BurgerMeal < NormalObject
  def initialize(fries = NullObject.new)
    @fries = fries
  end

  def sauce
    If.new(@fries, Ketchup.new).result
  end
end

BurgerMeal.new.sauce # ==> NullObject
BurgerMeal.new(Fries.new).sauce # ==> Ketchup


, : " , ?". ""?



:



# 
if today_is_friday?
  order_beers()
else
  order_tea()
end

#  If 
If.new(today_is_friday?, order_beers(), order_tea()).result


. - , .



() , .. .



. (. "callable"):



class OrderBeers
  def call
    # do something
  end
end

class OrderTea
  def call
    # do something else
  end
end

If.new(today_is_friday?, OrderBeers.new, OrderTea.new)
  .result
  .call


, , #call. . If.



( , )



(null ), . :



class Bool < NormalObject; end

class TrueObject < Bool; end

class FalseObject < Bool
  def if_branching(_then_val, else_val)
    else_val
  end
end


Bool, TrueObject ( , .. )

FalseObject, #if_branching , NullObject.



. . :



class BoolNot < Bool
  def initialize(x)
    @x = x
  end

  def if_branching(then_val, else_val)
    @x.if_branching(else_val, then_val)
  end
end


- "" #if_branching. , .





, — . . While.



:



while some_condition
  do_something
end


: " , ".



, — . !



class While < NormalObject
  def initialize(callable_condition, callable_body)
    @cond = callable_condition
    @body = callable_body
  end

  def run
    is_condition_satisfied = @cond.call
    If.new(is_condition_satisfied,
           NextIteration.new(self, @body),
           DoNothing.new)
      .result
      .call
  end

  #  ""    While#run.
  #     
  # (,     )
  class NextIteration < NormalObject
    def initialize(while_obj, body)
      @while_obj = while_obj
      @body = body
    end

    def call
      @body.call
      @while_obj.run
    end
  end

  class DoNothing < NormalObject
    def call
      NullObject.new
    end
  end
end




, null-.





:



class List < NormalObject
  def initialize(head, tail = NullObject.new)
    @head = head
    @tail = tail
  end

  def head
    @head
  end

  def tail
    @tail
  end
end


- ( #each !). , :



#
#     
#
class ListWalk < NormalObject
  def initialize(list)
    @left = list
  end

  def left
    @left
  end

  #        current.
  #  null   
  def next
    head = If.new(left, HeadCallable.new(left), ReturnNull.new)
             .result
             .call
    @left = If.new(left, TailCallable.new(left), ReturnNull.new)
              .result
              .call
    head
  end

  def finished?
    BoolNot.new(left)
  end

  class HeadCallable < NormalObject
    def initialize(list)
      @list = list
    end

    def call
      @list.head
    end
  end

  class TailCallable < NormalObject
    def initialize(list)
      @list = list
    end

    def call
      @list.tail
    end
  end

  class ReturnNull < NormalObject
    def call
      NullObject.new
    end
  end
end


, . #head #tail, null-pointer ( , null null, ).





, :



class Counter < NormalObject
  def initialize
    @list = NullObject.new
  end

  def inc
    @list = List.new(NullObject.new, @list)
  end

  class IncCallable < NormalObject
    def initialize(counter)
      @counter = counter
    end

    def call
      @counter.inc
    end
  end

  def inc_callable
    IncCallable.new(self)
  end
end


. ( ).



#inc_callable. , "" , , _callable . - .



null



null. NormalObject NullObject #null? ( #nil? Ruby):



class NormalObject < BaseObject
  def null?
    FalseObject.new
  end
end

class NullObject < BaseObject
  def null?
    TrueObject.new
  end
end


null-:



#
#  ,     NullObject  
#
class CountNullsInList < NormalObject
  def initialize(list)
    @list = list
  end

  def call
    list_walk = ListWalk.new(@list)
    counter = Counter.new

    While.new(ListWalkNotFinished.new(list_walk),
              LoopBody.new(list_walk, counter))
         .run

    counter
  end

  class ListWalkNotFinished < NormalObject
    def initialize(list_walk)
      @list_walk = list_walk
    end

    def call
      BoolNot.new(@list_walk.finished?)
    end
  end

  class LoopBody < NormalObject
    class ReturnNull < NormalObject
      def call
        NullObject.new
      end
    end

    def initialize(list_walk, counter)
      @list_walk = list_walk
      @counter = counter
    end

    def call
      x = @list_walk.next
      If.new(x.null?, @counter.inc_callable, ReturnNull.new)
        .result
        .call
    end
  end
end


. null- .





- — , , . , , (!), - . , : . — (, null, NullObject). , ...



experiments.




All Articles