Skip to content

Prototype-based object oriented programming in BQN

License

Notifications You must be signed in to change notification settings

MX-Futhark/object.bqn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

object.bqn

object.bqn is a library that brings prototype-based object oriented programming to BQN.

Examples

For full details on available features, please refer to the doc comments in the source code.

  • Empty object:
Object⟩ ← •Import "object.bqn"

oObject@
o.Entries@ # → ⟨⟩
  • Object with initial properties:
Object⟩ ← •Import "object.bqn"

oObject"a"1, "b"2o.Entries@ # → ⟨ ⟨ "a" 1 ⟩ ⟨ "b" 2 ⟩ ⟩
  • Has/Get/Set/Delete:
Object⟩ ← •Import "object.bqn"

oObject@
o.Has "key" # → 0
o.Set "key""value"
o.Get "key" # → "value"
o.Delete "key"
o.Has "key" # → 0
  • Methods:
Object⟩ ← •Import "object.bqn"

miloObject"name""Milo"
  "bark"‿{self 𝕊 times:
    (self.Get "name")  " says:"  {𝕩  " woof"}times ""
  }
⟩

•Out milo.Send"bark" 3 # → "Milo says: woof woof woof"
  • Prototype inheritance:
Object⟩ ← •Import "object.bqn"

dogObject"name""Some dog"
  "bark"‿{self 𝕊 times:
    (self.Get "name")  " says:"  {𝕩  " woof"}times ""
  }
⟩
miloObject"name""Milo"milo.SetPrototype dog

•Out dog.Send"bark" 3 # → "Some dog says: woof woof woof"
•Out milo.Send"bark" 3 # → "Milo says: woof woof woof"
  • Simulating classes:
Object⟩ ← •Import "object.bqn"

# Static members:
animalObject"class name""animal"# Class constructor:
animal.SetInitializer {{self 𝕊 name:
  self.Set "name"name
}}
# Instance members:
animal.SetInstancePrototype Object"name"‿@
  "cry""**silence**"
  "perform cry"‿{self 𝕊 times:
    (self.Get "name")  " says:"  {𝕩  " "  self.Get "cry"}times ""
  }
⟩

dogObject"class name""dog"dog.SetInitializer {{self 𝕊 name:
  selfanimal.initializer name
}}
# Inheritance:
dog.SetInstancePrototype {𝕩.SetPrototype animal.instancePrototype} Object"cry""woof"
  "perform cry"‿{self 𝕊 times:
    (milo.Send"bark only" times)  ", then growls"
  }
  "bark only"‿{self 𝕊 times:
    self(animal.instancePrototype.Get "perform cry") times
  }
⟩

milodog.New "Milo"

•Out milo.Send"perform cry" 3 # → "Milo says: woof woof woof, then growls"

About

Prototype-based object oriented programming in BQN

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published