Sha256: 43be1dbcdf6bb55a867ceb33d3046669f8e523a71bb5052fac75bb1ef84f6002

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

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

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

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

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

  [:bind, :>=].each do |m|
    # return a >>= f = f a
    describe "left identity" do
      context "when i have nothing" do
        it do
          expect(Nothing.public_send(m, &lifted_id)).to eq(Nothing)
        end
      end

      context "when i have just :x" do
        it do
          expect(unit(:x).public_send(m, &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.public_send(m, &lifted_id)).to eq(Nothing)
        end
      end

      context "when i have just :x" do
        it do
          expect(Just(:x).public_send(m, &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.public_send(m, &f).public_send(m, &g)
          rhs = Nothing.bind { |x| f.(x).public_send(m, &g) }
          expect(lhs).to eq(rhs)
        end
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ribimaybe-0.1.0 spec/monad_spec.rb