Module: Kernel

Defined in:
lib/lab42/data_class/kernel.rb,
lib/lab42/data_class/constraints/kernel.rb

Constant Summary collapse

Nil =
Lab42::Nil
Constraints =
Lab42::DataClass::Constraints
Constraint =
Constraints::Constraint
ListOfConstraint =
Constraints::ListOfConstraint
PairOfConstraint =
Constraints::PairOfConstraint
TripleOfConstraint =
Constraints::TripleOfConstraint
Maker =

TODO: Move Maker to Lab42::DataClass:ConstraintMaker

Lab42::DataClass::Proxy::Constraints::Maker
Anything =
Constraint.new(name: "Anything", function: ->(_) { true })
Boolean =
Constraint.new(name: "Boolean", function: -> { [false, true].member?(_1) })

Instance Method Summary collapse

Instance Method Details

#All?(constraint = nil, &blk) ⇒ Boolean

Returns:



14
15
16
17
18
19
20
# File 'lib/lab42/data_class/constraints/kernel.rb', line 14

def All?(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> do
    _1.all?(&constraint)
  end
  Constraint.new(name: "All?(#{constraint})", function: f)
end

#Any?(constraint = nil, &blk) ⇒ Boolean

Returns:



22
23
24
25
26
27
28
# File 'lib/lab42/data_class/constraints/kernel.rb', line 22

def Any?(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> do
    _1.any?(&constraint)
  end
  Constraint.new(name: "Any?(#{constraint})", function: f)
end

#Choice(*constraints) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/lab42/data_class/constraints/kernel.rb', line 30

def Choice(*constraints)
  constraints = constraints.map{ Maker.make_constraint _1 }
  f = ->(value) do
    constraints.any?{ _1.(value) }
  end
  Constraint.new(name: "Choice(#{constraints.join(', ')})", function: f)
end

#Contains(str) ⇒ Object



38
39
40
41
# File 'lib/lab42/data_class/constraints/kernel.rb', line 38

def Contains(str)
  f = -> { _1.include?(str) rescue false }
  Constraint.new(name: "Contains(#{str})", function: f)
end

#DataClass(*args, **kwds, &blk) ⇒ Object



7
8
9
10
# File 'lib/lab42/data_class/kernel.rb', line 7

def DataClass(*args, **kwds, &blk)
  proxy = Lab42::DataClass::Proxy.new(*args, **kwds, &blk)
  proxy.define_class!
end

#EndsWith(str) ⇒ Object



43
44
45
46
# File 'lib/lab42/data_class/constraints/kernel.rb', line 43

def EndsWith(str)
  f = -> { _1.end_with?(str) rescue false }
  Constraint.new(name: "EndsWith(#{str})", function: f)
end

#Lambda(arity) ⇒ Object



48
49
50
51
52
53
# File 'lib/lab42/data_class/constraints/kernel.rb', line 48

def Lambda(arity)
  function = -> do
    _1.arity == arity rescue false
  end
  Constraint.new(name: "Lambda(#{arity})", function:)
end

#List(*elements) ⇒ Object



12
13
14
# File 'lib/lab42/data_class/kernel.rb', line 12

def List(*elements)
  Lab42::List.new(*elements)
end

#ListOf(constraint, &blk) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/lab42/data_class/constraints/kernel.rb', line 55

def ListOf(constraint, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  function = -> do
    (Lab42::List === _1 || Lab42::Nil == _1) &&
    _1.all?(&constraint)
  end
  ListOfConstraint.new(name: "ListOf(#{constraint})", constraint:, function:)
end

#NilOr(constraint = nil, &blk) ⇒ Object



64
65
66
67
68
# File 'lib/lab42/data_class/constraints/kernel.rb', line 64

def NilOr(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> { _1.nil? || constraint.(_1) }
  Constraint.new(name: "NilOr(#{constraint})", function: f)
end

#Not(constraint = nil, &blk) ⇒ Object



70
71
72
73
74
# File 'lib/lab42/data_class/constraints/kernel.rb', line 70

def Not(constraint = nil, &blk)
  constraint = Maker.make_constraint(constraint, &blk)
  f = -> { !constraint.(_1) }
  Constraint.new(name: "Not(#{constraint})", function: f)
end

#Pair(first, second) ⇒ Object



16
17
18
# File 'lib/lab42/data_class/kernel.rb', line 16

def Pair(first, second)
  Lab42::Pair.new(first, second)
end

#PairOf(fst, snd) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/lab42/data_class/constraints/kernel.rb', line 76

def PairOf(fst, snd)
  fst_constraint = Maker.make_constraint(fst)
  snd_constraint = Maker.make_constraint(snd)
  constraint = [fst_constraint, snd_constraint]
  f = -> do
    Lab42::Pair === _1 && fst_constraint.(_1.first) && snd_constraint.(_1.second)
  end
  PairOfConstraint.new(name: "PairOf(#{fst_constraint}, #{snd_constraint})", function: f, constraint:)
end

#StartsWith(str) ⇒ Object



86
87
88
89
# File 'lib/lab42/data_class/constraints/kernel.rb', line 86

def StartsWith(str)
  f = -> { _1.start_with?(str) rescue false }
  Constraint.new(name: "StartsWith(#{str})", function: f)
end

#Triple(first, second, third) ⇒ Object



20
21
22
# File 'lib/lab42/data_class/kernel.rb', line 20

def Triple(first, second, third)
  Lab42::Triple.new(first, second, third)
end

#TripleOf(fst, snd, trd) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lab42/data_class/constraints/kernel.rb', line 91

def TripleOf(fst, snd, trd)
  fst_constraint = Maker.make_constraint(fst)
  snd_constraint = Maker.make_constraint(snd)
  trd_constraint = Maker.make_constraint(trd)
  constraint = [fst_constraint, snd_constraint, trd_constraint]
  f = -> do
    Lab42::Triple === _1 && fst_constraint.(_1.first) && snd_constraint.(_1.second) && trd_constraint.(_1.third)
  end
  TripleOfConstraint.new(
    name: "TripleOf(#{fst_constraint}, #{snd_constraint}, #{trd_constraint})",
    function: f,
    constraint:
  )
end