janet-lang / spork

Various Janet utility modules - the official "Contrib" library.
MIT License
124 stars 36 forks source link

Curious code in `math.janet` #176

Closed sogaiu closed 9 months ago

sogaiu commented 9 months ago

I noticed this line, this line, and these lines in math.janet:

  (def one (+ (- n n) 1))
  (def zero (- n n))
  (var d (- n 1))
  (def one (- n d))

Are these special optimization tricks @primo-ppcg?

primo-ppcg commented 9 months ago

Yes, these eliminate the need for polymorphic comparators, because one and n are of the same type.

sogaiu commented 9 months ago

Thanks for the explanation!

primo-ppcg commented 9 months ago

I suppose dynamic coercion might also be something of a lexical gap.

(def one (coerce 1 (type n)))

The intent would be a lot more clear.