dghosef / qdbp

MIT License
51 stars 1 forks source link

bug #68

Open dghosef opened 4 months ago

dghosef commented 4 months ago
abort: {
  msg: "Reached a fatal error. Aborting." Print.
  {self!.}!.
}

; Booleans
true: {
  Data[#True{}]
  If [then else|
    self Data.
      True? [then!.]
      False? [else!.].
  ]
}
false: {true Data[#False{}]}

; Optionals
none: {
  Data[#None{}]
  Transform[fn|
    self Data.
      None? [{self Data[#None{}]}]
      Some? [value| {self Data[#Some (fn! value)]}].
  ]
  If [then else|
    self Data.
      Some? [v| then! v.]
      None? [else!.].
  ]
}

some: {value| {none Data[#Some value]}}

; List data type
list: {
  Data[none]
  Length[0]
  Prepend[elem|
    new_data: some! { Front[elem] Rest[`Prepend Data.] }.
    new_size: self Length. + 1.
    {self Data[new_data] Length[new_size]}
  ]
  Concat[other| 
    concat: {lhs rhs| concat: self
      lhs If {v| some! {Front[v Front.] Rest[concat! (v Rest) rhs: rhs.]}.}
             else: {rhs}.
    }
    new_size: self Length. + (other Length).
    new_data: concat! (self Data) rhs: (other Data).
    {self Data[new_data] Length[new_size]}
  ]
  Map[fn|
    map: {lst| map: self
      lst Transform {v| {Front[(fn! (v Front).)] Rest[map! (v Rest).]}}.
    }
    new_data: map! (self Data).
    {self Data[new_data]}
  ]
  FoldLeft[fn init|
    fold: {lst acc| fold: self
      lst If {v| fold! (v Rest) acc: (fn! (v Front) acc: acc).}
             else: {acc}.
    }
    fold! (self Data) acc: init.
  ]
  FoldRight[fn init|
    fold: {lst acc| fold: self
      lst If {v| (fn! (v Front) acc: (fold! (v Rest) acc: acc).)}
             else: {acc}.
    }
    fold! (self Data) acc: init.
  ]
}
l1: list Prepend 3.
l2: l1 Concat l1. Prepend 4. Prepend 5.
l2 FoldLeft {x acc| x - acc.} init: 0. Print.

produces some really big negative number which is bad