janet-lang / spork

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

Curious code in `math.janet` #176

Closed sogaiu closed 8 months ago

sogaiu commented 8 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 8 months ago

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

sogaiu commented 8 months ago

Thanks for the explanation!

primo-ppcg commented 8 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.