Sha256: fc50befa5c2f85e82fb0a26095116d440ad9daf45b9af5856ee4fac56a12f1d5

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

require "spec_helper"
include Ribimaybe::Maybe
describe "Monad Instance" do
  let(:id) do
    ->(x) { x }
  end

  let(:lifted_id) do
    ->(x) { id.(rturn(x)) }
  end

  let(:f) do
    ->(x){ ->(y) { rturn(x) } }.(SecureRandom.base64(1000))
  end

  let(:g) do
    ->(x){ ->(y) { rturn(x) } }.(SecureRandom.base64(1000))
  end

  # return a >>= f = f a
  describe "left identity" do
    context "when i have nothing" do
      it do
        expect(Nothing.bind(&lifted_id)).to eq(Nothing)
      end
    end

    context "when i have just :x" do
      it do
        expect(rturn(:x).bind(&lifted_id)).to eq(lifted_id.(:x))
      end
    end
  end

  # m >>= return = m
  describe "right identity" do
    context "when i have nothing" do
      it do
        expect(Nothing.bind(&lifted_id)).to eq(Nothing)
      end
    end

    context "when i have just :x" do
      it do
        expect(Just(:x).bind(&lifted_id)).to eq(Just(:x))
      end
    end
  end

  # (m >>= f) >>= g = m >>= (\x -> f x >>= g)
  describe "associativity" do
    context "when i have nothing" do
      it do
        lhs = Nothing.bind(&f).bind(&g)
        rhs = Nothing.bind { |x| f.(x).bind(&g) }
        expect(lhs).to eq(rhs)
      end
    end

    context "when i have just :x" do
      it do
        lhs = Just(:x).bind(&f).bind(&g)
        rhs = Just(:x).bind { |x| f.(x).bind(&g) }
        expect(lhs).to eq(rhs)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ribimaybe-0.0.12 spec/monad_spec.rb
ribimaybe-0.0.11 spec/monad_spec.rb
ribimaybe-0.0.10 spec/monad_spec.rb