Skip to content

Instantly share code, notes, and snippets.

@maiha
Created November 18, 2020 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maiha/dfba565445bbe3b461670568aaf2cc8e to your computer and use it in GitHub Desktop.
Save maiha/dfba565445bbe3b461670568aaf2cc8e to your computer and use it in GitHub Desktop.

Typical Operations

a ?? b # => a.typical_zero? ? b : a

def T#typical_zero?
  self == T.typical_zero
def T.typical_zero
  nil

Logical Operations

if (a) # => if (! a.logical_zero?)

def T#logical_zero?
  false
def Nil#logical_zero?
  true

Semantic Operations

a || b # => a.semantic_zero? ? b : a

def T#semantic_zero?
  {% if T.respond_to?(:semantic_zero) %}
    self == T.semantic_zero
  {% else %}
    true

Arithmetic Operations

Enumerable(T)#sum   # => sum   Reflect(T).first.additional_zero
Enumerable(T)#multi # => multi Reflect(T).first.multiplicative_zero

stdlib

def String#logical_zero?       = false
def String.semantic_zero       = ""
def String.additional_zero     = ""
def String.multiplicative_zero = ""

"" ?? "no input" # => "" (due to typical zero)
"" || "no name"  # => "no name" (due to semantic zero)
if ""            # => true (due to logical_zero?)

def Bool#logical_zero?       = self
def Bool.semantic_zero       = false
def Bool.additional_zero     = false
def Bool.multiplicative_zero = true

if false            # => false
[false, true].sum   # => true
[false, true].multi # => false

user class (Not Empty List)

def Array(T)#semantic_zero? = false
def NEL(T)#semantic_zero?   = empty?

Array(Int32).new || [1] # => []
NEL(Int32).new   || [1] # => [1]
if NEL(Int32).new       # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment