Last active
July 26, 2019 13:15
-
-
Save brettrowberry/0b9047c77376dd3445c05e202664c11e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The 'compare' function from FSharp.Core returns a magic number to indicate the result | |
// compare : 'T -> 'T -> int when 'T : comparison | |
compare 0 1 //-1 | |
compare 0 0 //0 | |
compare 1 0 //1 | |
// We could clarify our intent | |
type Leg = Less | Equal | Greater | |
type Comparer<'T> = 'T -> 'T -> Leg | |
let myCompare a b = | |
if a < b then Less | |
elif a = b then Equal | |
else Greater | |
myCompare 0 1 //Less | |
myCompare 0 0 //Equal | |
myCompare 1 0 //Greater | |
let smaller (comparer : Comparer<'T>) a b = | |
match comparer a b with | |
| Less -> a | |
| Equal -> a | |
| Greater -> b | |
smaller myCompare 0 1 //0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I recently learned that the size of a boolean in Rust is 1 byte. I expected it to be a single bit or the size of an int. | |
In modern languages, we don't use special integer values of 0 and 1 to indicate boolean values. | |
However, we do use -1, 0, and 1 to communicate the results of comparison. | |
Is this suggestion a semantic improvement? | |
(Whitespace in the source file seems to be struggling, maybe look at the raw?) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment