Skip to content

Commit

Permalink
Implement Order and compare in Elm core libraries
Browse files Browse the repository at this point in the history
+ Add declarations for the `Order` type and `compare` function in the `Basics` module.
+ Add test steps to check the new `compare` function with integers, lists, and strings.
  • Loading branch information
Viir committed Jul 5, 2023
1 parent b017631 commit 778b055
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ type String
= String (List Char.Char)
{-| Represents the relative ordering of two things.
The relations are less than, equal to, and greater than.
-}
type Order = LT | EQ | GT
eq : a -> a -> Bool
eq a b =
Pine_kernel.equal [ a, b ]
Expand Down Expand Up @@ -147,6 +153,89 @@ not bool =
else
True
{-| Compare any two comparable values. Comparable values include `String`,
`Char`, `Int`, `Float`, or a list or tuple containing comparable values. These
are also the only values that work as `Dict` keys or `Set` members.
compare 3 4 == LT
compare 4 4 == EQ
compare 5 4 == GT
-}
compare : comparable -> comparable -> Order
compare a b =
if Pine_kernel.equal [ a, b ]
then
EQ
else
case a of
String stringA ->
case b of
String stringB ->
compareList
(stringCharsToSignedInts stringA)
(stringCharsToSignedInts stringB)
_ ->
compareIgnoringString a b
_ -> compareIgnoringString a b
compareIgnoringString : comparable -> comparable -> Order
compareIgnoringString a b =
if isPineList a
then
compareList a b
else if Pine_kernel.is_sorted_ascending_int [ a, b ]
then
LT
else
GT
compareList : List comparable -> List comparable -> Order
compareList listA listB =
let
compareListEmpty =
compare (Pine_kernel.length listA) (Pine_kernel.length listB)
in
case listA of
headA :: tailA ->
case listB of
headB :: tailB ->
let
headOrder =
compare headA headB
in
if eq headOrder EQ
then
compareList tailA tailB
else
headOrder
_ ->
compareListEmpty
_ ->
compareListEmpty
stringCharsToSignedInts : List Char -> List Int
stringCharsToSignedInts chars =
case chars of
head :: tail ->
-- Add the sign prefix byte
Pine_kernel.concat
[ [ Pine_kernel.concat [ Pine_kernel.take [ 1, 0 ], head ] ]
, stringCharsToSignedInts tail
]
_ ->
[]
isPineList a =
Pine_kernel.equal [ Pine_kernel.take [ 0, a ], [] ]
"""
, """
module Tuple exposing
Expand Down
2 changes: 1 addition & 1 deletion implement/elm-time/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ElmTime;

public class Program
{
public static string AppVersionId => "2023-07-01";
public static string AppVersionId => "2023-07-05";

private static int AdminInterfaceDefaultPort => 4000;

Expand Down
4 changes: 2 additions & 2 deletions implement/elm-time/elm-time.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>ElmTime</RootNamespace>
<AssemblyName>elm-time</AssemblyName>
<AssemblyVersion>2023.0701.0.0</AssemblyVersion>
<FileVersion>2023.0701.0.0</FileVersion>
<AssemblyVersion>2023.0705.0.0</AssemblyVersion>
<FileVersion>2023.0705.0.0</FileVersion>
<Nullable>enable</Nullable>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EQ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare 0 0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare 3 4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EQ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare 4 4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare 5 4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare [ 13, 17 ] [ 13, 21 ]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare [ 17, 17 ] [ 13, 21 ]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare "hello" "world"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basics.compare "hello" "elm"

0 comments on commit 778b055

Please sign in to comment.