Fun with operators, numeric units, and application in Kopi

2 views
Skip to first unread message

Mike Austin

unread,
Nov 14, 2021, 5:20:42 PM11/14/21
to PiLuD
I have to say, function application via juxtaposition (whitespace like Haskell) provides many opportunities to customize the language, almost to the point of a DSL.

print (4 ft + 6 in)
print (4 ft + 12.7 cm)

Number apply simply calls the argument with itself. Bam! Numeric units. String/Sequence apply calls concat.

Imperial = type (value: Int)

extend Imperial (
toImperial: (this) => () => this
+: (this) => (that) => Imperial (value: this.value + (that | toImperial).value)
)

ft (n) = Imperial (value: n)
in (n) = Imperial (value: n / 12)

print (4 ft + 6 in)


# Add Metric unit

Metric = type (value: Int)

m (n) = Metric (value: n)
cm (n) = Metric (value: n / 10)

extend Imperial (
toMetric: (this) => () => Metric (value: this.value * 2.54)
)

extend Metric (
toMetric: (this) => () => this
toImperial: (this) => () => Imperial (value: this.value / 2.54)
+: (this) => (that) => Imperial (value: this.value + (that | toMetric).value)
)

print (4 ft + 12.7 cm)

Raoul Duke

unread,
Nov 14, 2021, 5:44:53 PM11/14/21
to pi...@googlegroups.com
i do believe that is what the haskell people have been saying for quite a while, agreed. 

--
You received this message because you are subscribed to the Google Groups "PiLuD" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pilud+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pilud/b89fc4c1-0a16-4ff2-8429-34fc38b8a6e2n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages